package com.taxshare.manager.controller;

import com.taxshare.manager.entity.OrganizeEntity;
import com.taxshare.manager.service.OrganizeService;
import com.taxshare.manager.view.dto.OrganizeDTO;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: OrganizeController
 * @Description:
 * @author: luozhongyi
 * @date: 2021/2/3 10:02
 */
@RestController
@RequestMapping(value = "/organize")
@Api(tags = "组织管理")
public class OrganizeController {

    @Autowired
    private OrganizeService organizeService;

    @RequestMapping(method = RequestMethod.POST, value = "/add")
    @ApiOperation(notes = "组织信息", value = "新增")
    public Response add(@RequestBody OrganizeDTO organizeDTO) {
        return organizeService.add(organizeDTO);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/edit")
    @ApiOperation(notes = "组织信息", value = "编辑")
    public Response edit(@RequestBody OrganizeDTO organizeDTO) {
        return organizeService.edit(organizeDTO);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/delete")
    @ApiOperation(notes = "组织信息", value = "删除")
    public Response delete(@RequestBody OrganizeDTO organizeDTO) {
        return organizeService.delete(organizeDTO);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/info")
    @ApiOperation(notes = "组织信息", value = "单个组织查询")
    public Response info(String orgId) {
        return organizeService.info(orgId);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/initTree")
    @ApiOperation(notes = "组织信息", value = "初始化组织树")
    public Response initOrgTree() {
        return organizeService.initOrgTree();
    }

    @RequestMapping(method = RequestMethod.POST, value = "/queryOrganizeEntity")
    @ApiOperation(value = "父公司管理列表分页查询", notes = "账套信息")
    public Response queryOrganizeEntity(@RequestBody PageRequest<OrganizeEntity> query) {
        return organizeService.queryOrganizeEntityList(query);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getOrganizeByTaxNumber")
    @ApiOperation(value = "根据公司税号查账套管理列表", notes = "账套信息")
    public Response getOrganizeByTaxNumber(@RequestParam String taxNumber) {
        OrganizeEntity accounts = organizeService.getOrganizeByTaxNumber(taxNumber);
        return Response.success(accounts);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/queryCompanyName")
    @ApiOperation(value = "根据父公司id查子公司的对应列表", notes = "账套信息")
    public Response queryCompanyName(@RequestParam String orgPid) {
        return organizeService.queryCompanyName(orgPid);
    }

    @PostMapping("/updateOrganize")
    @ApiOperation(value = "修改", notes = "账套信息")
    public Response updateOrganize(@RequestBody OrganizeEntity entity) {
        return organizeService.updateOrganize(entity);
    }


    /**
     * 根据税号查询账户信息
     * TODO 2024/11/23 更改
     * @param taxNumber 税号
     * @return Response 返回查询结果
     */
    @RequestMapping(method = RequestMethod.GET, value = "/queryAccountList")
    @ApiOperation(value = "根据税号查询账户列表", notes = "通过税号查询关联的组织和公司名称")
    public Response queryAccountList(
            @ApiParam(value = "税号", required = true) @RequestParam String taxNumber) {
        if (taxNumber == null || taxNumber.trim().isEmpty()) {
            return Response.warn("税号不能为空！");
        }
        try {
            OrganizeEntity organizeEntity = this.organizeService.getOrganizeTaxList(taxNumber);
            if (organizeEntity == null) {
                return Response.warn("未查询到相关信息！");
            }
            return this.organizeService.queryCompanyName(organizeEntity.getOrgId());
        } catch (Exception e) {
            return Response.error("查询过程中发生错误：" + e.getMessage());
        }
    }
}


